package com.pruebas.xmlsimple.view;

import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.pruebas.xmlsimple.model.Team;

public class TeamListView extends ListView {

	public TeamListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	public TeamListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	public TeamListView(Context context) {
		super(context);
		init(context);
	}
	private void init(Context context){
		TeamsAdapter teamsAdapter = new TeamsAdapter();
		setAdapter(teamsAdapter);
	}
	
	public void setTeamList(List<Team> teamList){
		TeamsAdapter teamsAdapter = (TeamsAdapter)getAdapter();
		teamsAdapter.setTeamList(teamList);
	}
	
	private class TeamsAdapter extends BaseAdapter {
		
		private List<Team> list;
		
		public void setTeamList(List<Team> list){
			this.list = list;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			if(list != null){
				return list.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			if(list != null && position >= 0 && position < list.size()){
				return list.get(position);
			}
			
			return 0;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Object obj = getItem(position);
			TeamListItemView v = null;
			if(obj != null && obj instanceof Team){
				Team team = (Team)obj;
				
				if(convertView instanceof TeamListItemView){
					v = (TeamListItemView)convertView;
				}else{
					v =  new TeamListItemView(getContext());
				}
				v.setTeam(team);
			}
			return v;
		}
		
	}

}
